﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using ERP_RORZE_SYSTEM;
using System.Data;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class rpt_POCover : DevExpress.XtraReports.UI.XtraReport
    {
        public string po_id;
        public rpt_POCover()
        {
            InitializeComponent();
            this.po_id = null;            
        }

        public void BindData()
        {
            POID.DataBindings.Add("Text", DataSource, "POID");            
            SupplierName1.DataBindings.Add("Text", DataSource, "SupplierName");
            SupplierName.DataBindings.Add("Text", DataSource, "SupplierName");
            SupplierAddress.DataBindings.Add("Text", DataSource, "SupplierAddress");
            SupplierTelephone.DataBindings.Add("Text", DataSource, "SupplierTelephone");
            SupplierFax.DataBindings.Add("Text", DataSource, "SupplierFax");
            SupplierPICSex.DataBindings.Add("Text", DataSource, "SupplierPICSex");
            SupplierPIC1.DataBindings.Add("Text", DataSource, "SupplierPIC");
            SupplierPIC.DataBindings.Add("Text", DataSource, "SupplierPIC");
            PICPosition.DataBindings.Add("Text", DataSource, "PICPosition");
            NameOfComodity.DataBindings.Add("Text", DataSource, "NameOfCommodity");
            CurrencySymbol.DataBindings.Add("Text", DataSource, "CurrencySymbol");
            Amount.DataBindings.Add("Text", DataSource, "Amount");
            DeliveryTermName.DataBindings.Add("Text", DataSource, "DeliveryTermName");
            ShipToName.DataBindings.Add("Text", DataSource, "ShipToName");
            ShipToAddress.DataBindings.Add("Text", DataSource, "ShipToAddress");
            ShipToTelephone.DataBindings.Add("Text", DataSource, "ShipToTelephone");
            ShipToFax.DataBindings.Add("Text", DataSource, "ShipToFax");
            PaymentName.DataBindings.Add("Text", DataSource, "PaymentName");
            Remark.DataBindings.Add("Text", DataSource, "Remark");
        }

        public void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT t1.ID AS POID, t2.[SupplierName], t2.[Address] AS SupplierAddress,"
                + " t2.Telephone AS SupplierTelephone, t2.[Fax] AS SupplierFax, t2.[PICSex] AS SupplierPICSex,"
                + " t2.[PIC] AS SupplierPIC, t2.[PICPosition], t2.[NameOfCommodity], t4.[CurrencySymbol],"
                + " SUM(t3.[Quantity] * t3.UnitPrice) AS Amount, t5.[DeliveryTermName], t6.[ShipToName],"
                + " t6.[Address] AS ShipToAddress, t6.[Telephone] AS ShipToTelephone, t6.[Fax] AS ShipToFax,"
                + " t7.[PaymentName], t1.[Remark]"
                + " FROM [241_PO] AS t1 LEFT JOIN"
                + " [222_Supplier] AS t2 ON t1.[SupplierID]=t2.[SupplierID] LEFT JOIN"
                + " [241_PODetails] AS t3 ON t1.[ID]=t3.[POID] LEFT JOIN"
                + " [222_Currency] AS t4 ON t3.[CurrencyID]=t4.[CurrencyID] LEFT JOIN"
                + " [222_DeliveryTerm] AS t5 ON t1.[DeliveryTermID]=t5.[DeliveryTermID] LEFT JOIN"
                + " [222_ShipTo] AS t6 ON t1.[ShipTOCode]=t6.[ShipToCode] LEFT JOIN"
                + " [222_Payment] AS t7 ON t1.[PaymentID]=t7.[PaymentID]"
                + " WHERE t3.Cancelled=0 AND t3.Change=0"
                + " GROUP BY t1.ID, t2.[SupplierName], t2.[Address], t2.Telephone, t2.[Fax], t2.[PICSex], t2.[PIC],"
                + " t2.[PICPosition], t2.[NameOfCommodity], t4.[CurrencySymbol], t5.[DeliveryTermName], t6.[ShipToName],"
                + " t6.[Address], t6.[Telephone], t6.[Fax], t7.[PaymentName], t1.[Remark]"
                + " HAVING t1.ID='" + po_id + "'";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                try
                {
                    this.DataSource = sourceDataSet.Tables[0];
                    BindData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Cannot Load data to report");
            }
        }
    }
}
